﻿
Partial Class manage_users
    Inherits System.Web.UI.Page
#Region "Functions"
    Protected Sub GetAllUsers()
        Dim DataTable As MembershipUserCollection = Membership.GetAllUsers()
        Dim Row As MembershipUser
        Dim Odd As Boolean = False
        For Each Row In DataTable
            ListBox1.Items.Add(Row.UserName)
        Next
    End Sub
    Sub SetSelectedUserSetRole(User As String)
        Dim role As String()
        role = Roles.GetRolesForUser(User)
        If role.Count = 0 Then
            ListBox2.SelectedIndex = 4
        End If
        For Each item In role
            If item = "Manager" Then
                ListBox2.SelectedIndex = 0
            ElseIf item = "Modifier" Then
                ListBox2.SelectedIndex = 1
            ElseIf item = "User" Then
                ListBox2.SelectedIndex = 2
            ElseIf item = "Viewer" Then
                ListBox2.SelectedIndex = 3
            Else
                ListBox2.SelectedIndex = 4
            End If
        Next
    End Sub
#End Region

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If ListBox1.Items.Count < 1 Then
            GetAllUsers()
        End If
    End Sub

    Protected Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        SetSelectedUserSetRole(ListBox1.SelectedItem.Value)
        lblReturn.Text = ""
    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            Roles.AddUserToRole(ListBox1.SelectedValue, ListBox2.SelectedValue)
            lblReturn.Text = "Role Updated!"
        Catch ex As Exception
            lblReturn.Text = ex.Message
        End Try
    End Sub
End Class
